package edu.umn.se.trap.logicChecker;

import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import edu.umn.se.trap.Day;
import edu.umn.se.trap.Form;
import edu.umn.se.trap.Grant;
import edu.umn.se.trap.Incidental;
import edu.umn.se.trap.OtherExpenses;
import databases.GrantDB;
import databases.KeyNotFoundException;
import databases.GrantDB.GRANT_FIELDS;
import edu.umn.se.trap.exceptions.TrapException;

public class CheckLogicInternet implements Observer {

    @Override
    /**
     * Update for checking logic for internet
     */
    public void update(Observable o, Object arg) 
    {
        LogicChecker l1 = (LogicChecker) o;
        Form form = (Form) arg;
        GrantDB grantDB = new GrantDB();
        
        Vector<Grant> grants = form.getGrants();
        int count = 0;
        for (int i = 0; i < grants.size(); i++)
        {
            Grant grant = grants.elementAt(i);
            List<Object> grantInfo;
            
            try 
            {
                grantInfo = grantDB.getGrantInfo(String.valueOf(grant.getAccountNumber()));
                int index = GRANT_FIELDS.ACCOUNT_TYPE.ordinal();
                String fundingOrg = (String) grantInfo.get(index);
                if (fundingOrg.equalsIgnoreCase("sponsored"))
                {
                    count++;
                }
            } 
            catch (KeyNotFoundException e) {
                l1.setException(new TrapException("Key Invalid - Internet"));
            }
        }
        
        // if all listed grants are sponsored, then check to for internet or wifi
        if (count == grants.size())
        {
             Vector<Day> days = form.getDays();
             for (int i = 0; i < days.size(); i++)
             {
                Day today = days.get(i);
                Incidental incidental = today.getIncidentals();
                String justification = incidental.getJustification();
                if (!justification.equals("") && 
                        (justification.indexOf("internet") != -1 || justification.indexOf("Internet") != -1) &&
                           (justification.indexOf("wifi") != -1 || justification.indexOf("WiFi") != -1))
                {
                    l1.setException(new TrapException("Internet violation found"));
                }
             }
             
            Vector<OtherExpenses> otherExps = form.getOtherExpenses();
            for (int i = 0; i < otherExps.size(); i++)
            {
                OtherExpenses oExps = otherExps.get(i);
                String justification = oExps.getJustification();
                if (!justification.equals("") && 
                        (justification.indexOf("internet") != -1 || justification.indexOf("Internet") != -1) ||
                           (justification.indexOf("wifi") != -1 || justification.indexOf("WiFi") != -1))
                {
                    l1.setException(new TrapException("Internet violation found"));
                }
            }
        }
    }

}